﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;
using System.IO;
using SDK10kWrapper;
using System.Configuration;
using RS232;
using UI.Online.SubForms;

namespace UI.Online
{
    public partial class frmOnlineUI2
    {
        private bool StartRecord()
        {
            try
            {
                newRecord = new PRecord();
                currRecordID = tlvProjectEvent.AddRecord(newRecord, currPipelineID.ToString());
                // = lastRecordID + 1;

                string str = GetRecordFolder() + "Record#" + currRecordID.ToString();
                newRecord.Video4 = "";

                if (mnuSettingsCameras1.Checked)
                {
                    newRecord.Video1 = str + "_v1.avi";
                    cam1.SavePath = newRecord.Video1;
                    cam1.StartRecord();
                }
                if (mnuSettingsCameras2.Checked)
                {
                    newRecord.Video2 = str + "_v2.avi";
                    cam2.SavePath = newRecord.Video2;
                    cam2.StartRecord();
                }
                if (mnuSettingsCameras3.Checked)
                {
                    newRecord.Video3 = str + "_v3.avi";
                    cam3.SavePath = newRecord.Video3;
                    cam3.StartRecord();
                }
                t_start = DateTime.Now;
                stsCamera.Text = "All cameras are recording...";
                
                // Get data
                PData pdt = DataNow;
                pdt.DataTime = t_start;

                // Update newRecord info
                newRecord.TimeStart = t_start;
                newRecord.KPStart = pdt.KP;

                // Add start of survey event
                PEvent ss = new PEvent();
                ss = pdt.ToPEvent();
                ss.EventTime = t_start;
                ss.Description = "Start of survey";
                tlvProjectEvent.AddEvent(ss, currRecordID.ToString());
                tlvProjectEvent.RefreshTLV(currRecordID.ToString());

                DisableComboboxes();
                EnableEventButtons();
            }
            catch (Exception ex)
            {

                return false;
            }

            return true;
        }

        private bool StopRecord()
        {
            try
            {
                if (mnuSettingsCameras1.Checked)
                {
                    cam1.StopRecord();
                }
                if (mnuSettingsCameras2.Checked)
                {
                    cam2.StopRecord();
                }
                if (mnuSettingsCameras3.Checked)
                {
                    cam3.StopRecord();
                }
                t_end = DateTime.Now;
                stsCamera.Text = "Stop recording.";

                // Get data
                PData pdt = DataNow;

                // Add start of survey event
                PEvent es = pdt.ToPEvent();
                es.EventTime = t_end;
                es.Description = "End of survey";
                tlvProjectEvent.AddEvent(es, currRecordID.ToString());

                newRecord.TimeEnd = t_end;
                newRecord.KPEnd = pdt.KP;

                currRecordID = tlvProjectEvent.UpdateRecord(currRecordID, newRecord, currPipelineID.ToString());

                EnableComboboxes();
                DisableEventButtons();
            }
            catch (Exception ex)
            {
                return true;
            }

            return false;
        }

        private void LoadCamera()
        {
            cam1 = new Camera(panel1, "194.36.79.201", Camera1DefaultFilePath);
            cam2 = new Camera(panel2, "194.36.79.202", Camera2DefaultFilePath);
            cam3 = new Camera(panel3, "194.36.79.203", Camera3DefaultFilePath);
            if (mnuSettingsCameras1.Checked)
            {
                cam1.Connect();
                cam1.Play(false);
            }

            if (mnuSettingsCameras2.Checked)
            {
                cam2.Connect();
                cam2.Play(false);
            }

            if (mnuSettingsCameras3.Checked)
            {
                cam3.Connect();
                cam3.Play(false);
            }

            //cam1 = new Camera(panel1, "194.36.79.201", Camera1DefaultFilePath);
            //cam2 = new Camera(panel2, "194.36.79.202", Camera2DefaultFilePath);
            //cam3 = new Camera(panel3, "194.36.79.203", Camera3DefaultFilePath);

            //List<Camera> fails;
            //if (Camera.ConnectAll(out fails)) stsCamera.Text = "All Cameras is connected.";
            //else
            //{
            //    string s_fail = "";
            //    foreach (Camera failcam in fails)
            //    {
            //        s_fail += ", " + failcam.Name;
            //    }
            //    MessageBox.Show(
            //        "Cannot connect to video servers\nPlease check the connections.",
            //        "Error",
            //        MessageBoxButtons.OK,
            //        MessageBoxIcon.Exclamation,
            //        MessageBoxDefaultButton.Button1);
            //    stsCamera.Text = ("Can not connect to All Camera" + s_fail + " Fail !");
            //}
            //Camera.PlayAll(out fails, false); // Call to Static Method of Class will play all instance at the same time.
        }

        private void LoadProject()
        {
            cbbProject.Items.Clear();
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Projects";
            DataTable dt = dh.Fill();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["ProjectName"].ToString();
                cbbProject.Items.Add(name);
            }
            cbbProject.Text = currProjectName;
        }

        private void LoadPipeline()
        {
            cbbPipeline.Items.Clear();
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Pipelines WHERE ProjectID = " + currProjectID;
            DataTable dt = dh.Fill();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["PipelineName"].ToString();
                cbbPipeline.Items.Add(name);
            }
        }
    }
}
